/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.noah.controller;

import com.noah.model.UserModel;
import com.noah.config.LoadConfiguration;
import com.noah.service.RoleService;
import com.noah.service.UserService;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author ngoc.phan
 */
@Controller
public class UserController {

    UserService userService = (UserService) LoadConfiguration.loadJDBC().getBean("userDAO");
    RoleService roleService = (RoleService) LoadConfiguration.loadJDBC().getBean("roleDAO");

    @RequestMapping("/userdetails")
    public ModelAndView getUsers() {
        //UserService userService = new UserService();
        //List<LoginForm> listuser = userService.viewUser();
        ModelAndView mav = new ModelAndView("userdetails");
        mav.addObject("listUser", userService.viewUser());
        return mav;
    }
    /*
     * Insert user
     */

    @RequestMapping(value = "/insertuser", method = RequestMethod.GET)
    public String showinsertUser(ModelMap model) {
        UserModel usermodel = new UserModel();
        model.put("list", roleService.getRoles());
        model.put("insertUser", usermodel);
        return "insertuser";
    }

    @RequestMapping(value = "/insertuser", method = RequestMethod.POST)
    public ModelAndView insertUser(@ModelAttribute("insertUser") UserModel usermodel,
            BindingResult result, ModelMap model) {
        UserModel user = new UserModel();
        user.setUserName(usermodel.getUserName());
        user.setPassword(usermodel.getPassword());
        user.setRole(usermodel.getRole());
        userService.insertUser(user);
        return new ModelAndView("userdetails", "listUser", userService.viewUser());
    }

    /*
     * Seaching user
     */
    @RequestMapping(value = "/searchuser", method = RequestMethod.GET)
    public String showSearchUser(ModelMap model) {
        UserModel usermodel = new UserModel();
        model.put("searchinguser", usermodel);
        return "searchuser";
    }

    @RequestMapping(value = "/searchuser", method = RequestMethod.POST)
    public ModelAndView processSearchUser(@ModelAttribute("searchinguser") UserModel usermodel,
            ModelMap model) {
        return new ModelAndView("updateuser", "listUser", userService.viewUserByName(usermodel.getUserName()));
    }
    /*
     * Update user
     */

    @RequestMapping(value = "/updateuser", method = RequestMethod.GET)
    public String showUpdateUser(ModelMap model) {
        //UserModel user = new UserModel();
        model.put("listUser", userService.viewUser());
        return "updateuser";
    }

    @RequestMapping(value = "/updateuser", method = RequestMethod.POST)
    public String processUpdateUser(@ModelAttribute("listUser") UserModel usermodel) {
        UserModel user = new UserModel();
        user.setUserId(usermodel.getUserId());
        user.setUserName(usermodel.getPassword());
        user.setPassword(usermodel.getPassword());
        userService.updateUser(user);
        return "redirect:/userdetails";
        //return new ModelAndView("userdetails", "listUser", userService.viewUser());
    }
}
